package com.example.asyncktask;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

public class HttpUtil {

	private LiShiBean liShiBean;

	public List<LiShiBean> getLiShiResult_post(String url) throws Exception {
		List<LiShiBean> result = new ArrayList<LiShiBean>();
		HttpGet httpget = new HttpGet(url);
		try {
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
			httpget.addHeader("Context-Type", "text/html");
			httpget.addHeader("charset", HTTP.UTF_8);
			((HttpResponse) httpget).setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));
		} catch (Exception e) {
		}
		try {
			HttpResponse response = new DefaultHttpClient().execute(httpget);
			if (response.getStatusLine().getStatusCode() == 200) {
				StringBuilder builder = new StringBuilder();
				BufferedReader bufferedReader2 = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
				for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2.readLine()) {
					builder.append(s);
				}
				String reslutstr = builder.toString();
				try {
					XmlPullParserFactory pullParserFactory = XmlPullParserFactory.newInstance();
					XmlPullParser xmlPullParser = pullParserFactory.newPullParser();
					InputStream inputStream = new ByteArrayInputStream(reslutstr.getBytes());
					xmlPullParser.setInput(inputStream, "UTF-8");
					int eventType = xmlPullParser.getEventType();
					try {
						while (eventType != XmlPullParser.END_DOCUMENT) {
							String nodeName = xmlPullParser.getName();
							switch (eventType) {
							case XmlPullParser.START_DOCUMENT:
								result = new ArrayList<LiShiBean>();
								break;
							case XmlPullParser.START_TAG:
								if ("News".equals(nodeName)) {
									liShiBean = new LiShiBean();
								} else if ("NewsTitle".equals(nodeName)) {
									liShiBean.setNewsTitle(xmlPullParser.nextText());
								} else if ("NewsTime".equals(nodeName)) {
									liShiBean.setNewsTime(xmlPullParser.nextText());
								} else if ("NewsContent".equals(nodeName)) {
									liShiBean.setNewsContent(xmlPullParser.nextText());
								}  else if ("author".equals(nodeName)) {
									liShiBean.setAuthor(xmlPullParser.nextText());
								} 
								break;
							case XmlPullParser.END_TAG:
								if ("News".equals(nodeName)) {
									result.add(liShiBean);
									Log.i("temp", "解析后的数据:" + liShiBean.toString());
								}
								break;
							default:
								break;
							}
							eventType = xmlPullParser.next();
						}
					} catch (NumberFormatException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
				} catch (XmlPullParserException e) {
					e.printStackTrace();
				}
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
}
